package com.qf.utils;

import com.qf.entity.Book;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ReadBooksUtils {
    private Integer batchNum = 1000;
    private String filepath;
    private BufferedReader bufferedReader;

    public ReadBooksUtils(String filepath) throws FileNotFoundException {
        this.filepath = filepath;
        this.bufferedReader = new BufferedReader(new FileReader(filepath));
    }

    public ReadBooksUtils(Integer batchNum, String filepath) throws FileNotFoundException {
        this.batchNum = batchNum;
        this.filepath = filepath;
        this.bufferedReader = new BufferedReader(new FileReader(filepath));
    }

    public List<Book> readBatch() throws IOException {
//        final CsvReader csvRows = new CsvReader(new File(filepath),batchNum,);
        final ArrayList<Book> books = new ArrayList<>();

        String line = null;
        while ((line = bufferedReader.readLine())!=null){
            final Book book = Book.csvToBook(line);
            books.add(book);
            if (books.size() == batchNum){
                return books;
            }
        }
        return books;
    }
    public void close() throws IOException {
        this.bufferedReader.close();
    }
}
